﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace NetDiskPRJ.NetDisk
{
    class DownloadFileClient
    {
        public DownloadFileClient()
        {

        }
        public List<string> idList
        {
            set;
            get;
        }
        public List<string> nameList
        {
            set;
            get;
        }
        public List<string> typeList
        {
            set;
            get;
        }
        /// <summary>
        /// 下载路径保存参数
        /// </summary>
        public string downloadDestPath
        {
            set;
            get;
        }


        public List<Model.NetDisk> downloadNetDiskList
        {
            set;
            get;
        }




        /// <summary>
        /// 下载单个文件
        /// </summary>
        /// <param name="SrcPath">服务器文件路径（绝对路径）</param>
        /// <param name="DestPath">下载目的路径（绝对路径）</param>
        /// <param name="offset">分段大小，默认1M</param>
        public void DownloadSingle(string SrcPath, string DestPath, int offset = 1048576)
        {
           
            //获取远程文件的数据流
            string url = Base.GlobalParams.NetDiskServiceSVC.GetDownloadPostUrl();
            url += "?SrcPath=" + System.Web.HttpUtility.UrlEncode(SrcPath);
            FileStream fs = new FileStream(DestPath, FileMode.Create);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();

            byte[] bytes = new byte[offset];//根据分段大小下载

            int length = stream.Read(bytes, 0, offset);

            while (length > 0)
            {
                MainForm.mre2.WaitOne();
                fs.Write(bytes, 0, length);
                length = stream.Read(bytes, 0, offset);
            }
            stream.Close();
            fs.Close();
            response.Close();

        }








    }
}
